package com.briup.mt.web.controller;


import com.briup.mt.bean.Interfaces;
import com.briup.mt.service.IInterfacesService;
import com.briup.mt.utils.ExcelUtils;
import com.briup.mt.utils.Message;
import com.briup.mt.utils.MessageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Validated
@RequestMapping("/interfaces")
public class InterfacesController {

    @Autowired
    private IInterfacesService interfacesService;
    @ApiOperation(value = "导入到Excel中", notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "interfaces/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "接口名称",  paramType = "query"), })
    public void download(HttpServletResponse response, String name, String status) throws Exception {
        // 2. 将excel导出
        // 2.1 excel头部数据
        String[] headList = new String[] { "名称", "状态", "url", "限制时间", "用户名","模型ID","用户ID" };
        // 2.2 属性
        String[] fieldList = new String[] { "name", "status", "url", "limitTimes", "userName","moduleId","userId" };
        // 2.3 数据列，需要转化
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<Interfaces> list = interfacesService.query(name,status);
        for (Interfaces app : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", app.getName());
            map.put("status",app.getStatus());
            map.put("url",app.getUrl());
            map.put("limitTime",app.getLimitTimes());
            map.put("userName",app.getUserName());
            map.put("moduleId",app.getModuleId());
            map.put("userId",app.getUserId());
            dataList.add(map);
        }
        // 2.4 导出文件名
        String excelName = "interfaces";
        // 3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);

    }
    @GetMapping("query")
    public Message query(String name, String status){
        List<Interfaces> list = interfacesService.query(name,status);
        return MessageUtil.success(list);
    }


    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Interfaces face){
        interfacesService.saveOrUpdate(face);
        return MessageUtil.success("操作成功");
    }

    @DeleteMapping("deleteById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户编号", required = true, paramType = "query"),
    })
    public Message deleteById(@NotNull long id){
        interfacesService.deleteById(id);
        return MessageUtil.success("操作成功");
    }

}
